package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketStorage;
import com.cskaoyan.market.db.domain.MarketStorageExample;
import com.cskaoyan.market.db.mapper.MarketStorageMapper;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;



import com.cskaoyan.market.db.mapper.MarketStorageMapper;
import com.cskaoyan.market.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

public class MarketStorageServiceImpl implements MarketStorageService {

    @Override
    public List<MarketStorage> list(Integer limit, Integer page, String name, String key, String sort, String order) {
        SqlSession session = MybatisUtils.getSession();
        MarketStorageMapper mapper = session.getMapper(MarketStorageMapper.class);
        MarketStorageExample marketStorageExample = new MarketStorageExample();
        marketStorageExample.setOrderByClause(sort + " " + order);
        MarketStorageExample.Criteria criteria = marketStorageExample.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(key)) {
            criteria.andNameLike("%" + key + "%");
        }
        PageHelper.startPage(page, limit);
        List<MarketStorage> marketStorageList = mapper.selectByExampleSelective(marketStorageExample, MarketStorage.Column.id, MarketStorage.Column.key, MarketStorage.Column.name,
                MarketStorage.Column.type, MarketStorage.Column.size, MarketStorage.Column.url, MarketStorage.Column.addTime, MarketStorage.Column.updateTime, MarketStorage.Column.deleted);
        session.commit();
        session.close();
        return marketStorageList;
    }
    @Override
    public void create(MarketStorage marketStorage) {
        SqlSession session = MybatisUtils.getSession();
        MarketStorageMapper storageMapper = session.getMapper(MarketStorageMapper.class);
        storageMapper.insert(marketStorage);
        session.commit();
        session.close();
    }

    @Override
    public void update(MarketStorage marketStorage) {
        SqlSession session = MybatisUtils.getSession();
        MarketStorageMapper mapper = session.getMapper(MarketStorageMapper.class);
        mapper.updateByPrimaryKeySelective(marketStorage);
        session.commit();
        session.close();
    }

    @Override
    public void delete(MarketStorage marketStorage) {
        SqlSession session = MybatisUtils.getSession();
        MarketStorageMapper mapper = session.getMapper(MarketStorageMapper.class);
        mapper.deleteByPrimaryKey(marketStorage.getId());
        session.commit();
        session.close();
    }
}

